package com.tencent.qf.common.gateway.activitymanager;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

/**
 * 签到活动管理平台，接口交互类
 * @author liuxuan
 * @time 2022/12/27 11:02
 */
@Slf4j
@Component
public class ActivityManagerClient {
    @Resource
    private RestTemplate httpClientTemplate;
    @Autowired
    private ActivityManagerProperties activityManagerProperties;

    public String post(Object request, String apiPath) {
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Type", "application/json");
        headers.add("isPriority", activityManagerProperties.getIsPriority());
        HttpEntity<String> entity = new HttpEntity<>(JSONObject.toJSONString(request), headers);
        log.info("请求签到活动管理平台{} 入参{}", apiPath, JSON.toJSONString(entity));
        ResponseEntity<String> responseEntity = httpClientTemplate.postForEntity(activityManagerProperties.getUrl() + apiPath, entity, String.class);
        log.info("请求签到活动管理平台{} 出参{}", apiPath, JSON.toJSONString(responseEntity));
        if(responseEntity.getStatusCode() == HttpStatus.OK){
            String body = responseEntity.getBody();
            return body;
        }
        return null;
    }
}
